package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.business.domain.entity.AccountBookEntity;
import com.ruoyi.business.domain.form.AccountBookForm;
import com.ruoyi.business.domain.query.AccountBookQuery;
import com.ruoyi.business.domain.vo.AccountBookVo;
import com.ruoyi.business.mapper.AccountBookMapper;
import com.ruoyi.business.service.AccountBookService;
import com.ruoyi.common.utils.XtPageUtil;
import com.ruoyi.common.utils.bean.BeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.stream.Collectors;


/**
 * accountBook实现类
 */
@Service
@RequiredArgsConstructor
public class AccountBookServiceImpl extends ServiceImpl<AccountBookMapper, AccountBookEntity> implements AccountBookService {

    private final AccountBookMapper accountBookMapper;

    @Override
    public Result add(AccountBookForm form) {
        AccountBookEntity entity = new AccountBookEntity();
        BeanUtils.copyProperties(form,entity,"id");
        if(accountBookMapper.insert(entity)>0){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @Override
    public Result update(AccountBookForm form) {
        AccountBookEntity entity = accountBookMapper.selectById(form.getId());
        // 为空抛IllegalArgumentException，做全局异常处理
        Assert.notNull(entity, "记录不存在");
        BeanUtils.copyProperties(form, entity);
        if (accountBookMapper.updateById(entity) > 0) {
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }

    @Override
    public Result remove(List<String> ids) {
        if(baseMapper.deleteBatchIds(ids) > 0) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @Override
    public Result page(AccountBookQuery query) {
        IPage<AccountBookEntity> page = new LambdaQueryChainWrapper<>(accountBookMapper)
                .eq(StringUtils.hasText(query.getStatus()),AccountBookEntity::getStatus,query.getStatus())
                .orderByDesc(AccountBookEntity::getCreateTime)
                .page(XtPageUtil.getPage(query, AccountBookEntity.class));
        List<AccountBookVo> vos = page.getRecords().stream()
                .map(entity -> {

                    AccountBookVo vo = new AccountBookVo();
                    BeanUtils.copyProperties(entity, vo);
                    return vo;
                })
                .collect(Collectors.toList());
        return Result.success(vos).putTotal(page.getTotal());
    }

    @Override
    public Result detail(String id) {
        AccountBookEntity entity = accountBookMapper.selectById(id);
        Assert.notNull(entity,"记录不存在");
        AccountBookVo vo = new AccountBookVo();
        BeanUtils.copyProperties(entity, vo);
        return Result.success().putData(vo);
    }

    @Override
    public Result list(AccountBookQuery query) {
        List<AccountBookEntity> entities = new LambdaQueryChainWrapper<>(accountBookMapper)
                .eq(StringUtils.hasText(query.getStatus()),AccountBookEntity::getStatus, query.getStatus())
                .orderByDesc(AccountBookEntity::getCreateTime)
                .list();
        List<AccountBookVo> vos = entities.stream().map(entity -> {
                    AccountBookVo vo = new AccountBookVo();
                    BeanUtils.copyProperties(entity, vo);
                    return vo;
                })
                .collect(Collectors.toList());
        return Result.success().putData(vos);
    }

}